﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using System.Text;

public partial class BaseData_AddClothesCollocation : System.Web.UI.Page
{
    private string ClothesCollocationImageAbsolutePath
    {
        get
        {
            string clothesCollocationImageAbsolutePath = "";
            if (ViewState["ClothesCollocationImageAbsolutePath"] != null)
            {
                clothesCollocationImageAbsolutePath = ViewState["ClothesCollocationImageAbsolutePath"].ToString();
            }
            return clothesCollocationImageAbsolutePath;
        }
        set
        {
            ViewState["ClothesCollocationImageAbsolutePath"] = value;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200300111");
        if (!IsPostBack)
        {
            BindDropCollocationType();
        }
    }

    private void BindDropCollocationType()
    {
        DataTable dtCollocationType = new BLL.DPCategory().GetAllDpCategory().Tables[0];
        dropCollocationType.DataSource = dtCollocationType;
        dropCollocationType.DataTextField = "DP_NAME";
        dropCollocationType.DataValueField = "DP_ID";
        dropCollocationType.DataBind();
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300111", "00006"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (File1.Value.Trim() == "")
        {
            imgCollocation.ImageUrl = "";
            this.ClothesCollocationImageAbsolutePath = "";
            Share.Alert(Page, "请选择要上传的文件！"); return;
        }

        string filePath = File1.PostedFile.FileName;
        string type = File1.PostedFile.ContentType;
        string filename = DateTime.Now.Ticks.ToString();
        string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();

        #region 上传图片
        System.Drawing.Image m_Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream, true);

        Model.SYSPARAM syspa = new BLL.SYSPARAM().GetModel("001");
        Model.SYSPARAM syspa1 = new BLL.SYSPARAM().GetModel("008");
        if (syspa.SP_PARAM1 == 1)
        {
            string ftpip = syspa.SP_PARAM13;
            string ftpuser = syspa.SP_PARAM14;
            string ftppwd = syspa.SP_PARAM15;
            FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
            bool result = false;

            filename = DateTime.Now.Ticks.ToString();

            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension), Int32.Parse(syspa1.SP_PARAM19.Split(',')[0]), Int32.Parse(syspa1.SP_PARAM19.Split(',')[1]));
            // 把上传的文件写入磁盘

            result = false;
            if (!fud.IsExistsDirectory("", "ClothesCollocationImage", out result))
            {
                if (result == false)
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
                if (fud.MakeDir("", "ClothesCollocationImage"))
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }
            else
            {
                if (result == false)
                {
                    imgCollocation.ImageUrl = "";
                    this.ClothesCollocationImageAbsolutePath = "";
                    Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                }
            }

            if (fud.Upload("ClothesCollocationImage/", HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension), filename + filenameExtension))
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension)))
                {
                    File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension));
                }
                imgCollocation.ImageUrl = BLL.Common.GetProductCollocationPicPath() + filename + filenameExtension;
                this.ClothesCollocationImageAbsolutePath = filename + filenameExtension;
                Share.Alert(this.Page, "上传成功！");
            }
            else
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension)))
                {
                    File.Delete(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension));
                }
                imgCollocation.ImageUrl = "";
                this.ClothesCollocationImageAbsolutePath = "";
                Share.Alert(this.Page, "上传失败");
            }
        }
        else
        {
            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension), Int32.Parse(syspa1.SP_PARAM19.Split(',')[0]), Int32.Parse(syspa1.SP_PARAM19.Split(',')[1]));
            if (File.Exists(HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ClothesCollocationImage/" + filename + filenameExtension)))
            {
                imgCollocation.ImageUrl = BLL.Common.GetExpressPicPath() + filename + filenameExtension;
                this.ClothesCollocationImageAbsolutePath = filename + filenameExtension;
                Share.Alert(this.Page, "上传成功！");
            }
            else
            {
                imgCollocation.ImageUrl = "";
                this.ClothesCollocationImageAbsolutePath = "";
                Share.Alert(this.Page, "上传失败");
            }
        }
        #endregion
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200300111", "00001"))
        {
            Share.Alert(Page, "无权操作"); return;
        }
        if (txtCollocationName.Text == "")
        {
            Share.Alert(this.Page, "请填写搭配名称！"); return;
        }
        if (this.ClothesCollocationImageAbsolutePath == "")
        {
            Share.Alert(this.Page, "请上传搭配图片！"); return;
        }
        if (Hidden1.Value == "")
        {
            Share.Alert(this.Page, "请选择搭配包含的商品！"); return;
        }

        string newCollocationId = new BLL.ProductsDP().GetNewCollocationId();
        BLL.Common.ADDLSH("200300111");

        StringBuilder sbSql = new StringBuilder();
        sbSql.Append("INSERT INTO [ProductsDP]([PDP_ID],[PDP_Name],[PDP_Note],[PDP_Photo],[PDP_ISDEL],[PDP_Create],[DP_Type]) VALUES('");
        sbSql.Append(newCollocationId);
        sbSql.Append("','");
        sbSql.Append(txtCollocationName.Text);
        sbSql.Append("','");
        sbSql.Append(txtCollocationDescription.Text);
        sbSql.Append("','");
        sbSql.Append(this.ClothesCollocationImageAbsolutePath);
        sbSql.Append("',");
        sbSql.Append(rdoYes.Checked ? 1 : 0);
        sbSql.Append(",'");
        sbSql.Append(DateTime.Now);
        sbSql.Append("',");
        sbSql.Append(dropCollocationType.SelectedValue);
        sbSql.Append(")|");

        
        char[] charToTrim = new char[] { ',' };
        string[] arrProducts = Hidden1.Value.TrimEnd(charToTrim).Split(charToTrim);
        for (int i = 0; i < arrProducts.Length; i++)
        {
            string[] arrProductProperty = arrProducts[i].Split('~');
            if (arrProductProperty.Length >= 2)
            {
                sbSql.Append("INSERT INTO [ProductsDPMX]([PMX_ID],[PMX_MID],[ProductID],[PMX_GG1])VALUES('");
                sbSql.Append(newCollocationId);
                sbSql.Append("','");
                sbSql.Append(i + 1);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[0]);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[1]);
                sbSql.Append("')|");
            }
            else
            {
                sbSql.Append("INSERT INTO [ProductsDPMX]([PMX_ID],[PMX_MID],[ProductID],[PMX_GG1])VALUES('");
                sbSql.Append(newCollocationId);
                sbSql.Append("','");
                sbSql.Append(i + 1);
                sbSql.Append("','");
                sbSql.Append(arrProductProperty[0]);
                sbSql.Append("','1')|");
            }
        }

        string stringSql = sbSql.ToString();
        char[] charArray = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
        if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
        {
            Share.InsertLog("200300111", "00001");
            Response.Write("<script>alert('新增搭配成功！');window.location.href='ClothesCollocationsMain.aspx?code=" + newCollocationId + "'</script>");
        }
        else
        {
            Share.Alert(this.Page,"新增搭配失败！");
        }


    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
}
